package com.wangmei.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wangmei.model.OrderProduct;
import com.wangmei.model.Orderdetail;
import com.wangmei.pojo.OrderAddr;
import com.wangmei.service.OrderService;
import com.wordnik.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import unitTool.Dictionary;
import unitTool.JsonRsp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单的支付页面
 */
@RequestMapping("/pay")
@RestController
@Api(value = "12",description = "order")
public class OrderController {
    private final Logger logger = LoggerFactory.getLogger(OrderController.class);
    @Autowired
    private OrderService orderService;
    /**
     * 注：缺少表orderdetail
     * 结算页面
     * 结算字段，添加记录使用(order_product)
     * @param uid 添加新记录 ostate为 1
     *             传入参数为uid或者为detailid
     * @return
     *
     * 考虑跳转页面时机问题
     */
    @ApiOperation(value="test")
    @RequestMapping(value = "/check", method = RequestMethod.POST)
    public JsonRsp check(@RequestParam("uid") int uid, @RequestBody List<Orderdetail> orderDetails
            , @RequestBody OrderProduct orderProduct){
        return null;
    }


    /**
     * 付款，修改orderProduct的状态
     *  删除购物车里的物品(order_detail)
     * @Param 还有其他参数，自己加
     * @return
     */
    @RequestMapping(value = "/pay",method = RequestMethod.POST)
    public JsonRsp pay(@RequestBody List<Integer> detailId){
        return null;
    }


    //search
    /**
     * 获得order列表，管理员¥
     * @param state 状态值：0 购物车，1：核对信息，2：已支付
     * @param oid   根据id来获取订单号
     * @return
     */
    @RequestMapping(value = "/getOrder",method = RequestMethod.GET)
    public Object getAllOrder(
            @RequestParam(value = "uid", required = false) Integer uid //用户id
            ,@RequestParam(value = "state",defaultValue = "(1,2)") String state//订单状态
            ,@RequestParam(value = "oid",required = false)Integer oid//订单id
            ,@RequestParam(value = "addressid",required = false) Integer addressid//地址id
            ,@RequestParam(value = "search",required = false) String key
            ,@RequestParam(value = "pageIndex",defaultValue = Dictionary.PageDirect.PAGE_INDEX)
                    int pageIndex
            ,@RequestParam(value = "pageSize", defaultValue = Dictionary.PageDirect.PAGE_SIZE)
                    int pageSize ){
        try{
            Map<String,Object> map = new HashMap<>();
            if(uid != null)
                map.put("uid",uid);
            if(!"(1,2)".equals(state))
                state = getState(state);
            map.put("state","(2)");
            if(oid != null)
                map.put("oid",oid);
            if(addressid != null)
                map.put("addressid",addressid);
            if(key!=null) {
                System.out.println(key);
                key =  new String(key.getBytes("ISO8859-1"),"utf-8");
                System.out.println(key);
                map.put("key", key);
            }
            PageInfo page = getOrderPage(map,pageIndex,pageSize);
            return JsonRsp.success(page.getList()).setTotal(page.getTotal());
        }catch(Exception e){
            e.printStackTrace();
            return JsonRsp.fail(40000,"系统内部错误");
        }

    }

    private PageInfo getOrderPage(Map map,int pageIndex, int pageSize) throws Exception{
//        try{}catch(Exception e){}
        PageHelper.startPage(pageIndex,pageSize);
        List<OrderAddr> list = orderService.queryAllOrder(map);
        for(OrderAddr oa:list)
            oa.setStateString(Dictionary.Order.stateSTring(oa.getOstate()));
//        System.out.println(list);
        return new PageInfo(list);
    }

    private String getState(String state){
        int len = state.length();
        StringBuilder re =new StringBuilder();
        re.append("(");
        for(int i = 0;i<len;i++) {
            if(i != 0)
                re.append(",");
            re.append(state.charAt(i));
        }
        re.append(")");
        return re.toString();
    }
}
